package com.wiloon.java8x.io.nio;/*
 * Created by roy.wang on 2017/4/20;
 */

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

public class FileChannelX {
    public static void main(String[] args) throws IOException {
        URL url = ClassLoader.getSystemClassLoader().getResource("baidu.txt");
        FileOutputStream output = new FileOutputStream(url.getFile());
        FileChannel fileChannel = output.getChannel();
        URL baidu = new URL("http://www.baidu.com");
        InputStream input = baidu.openStream();
        ReadableByteChannel readChannel = Channels.newChannel(input);
        fileChannel.transferFrom(readChannel, 0, Integer.MAX_VALUE);
    }
}
